package com.darly.book.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.darly.book.BookController;
import com.darly.book.R;
import com.darly.book.event.BookSubSortEvent;
import com.darly.book.model.bean.SortBookBean;
import com.darly.book.model.flag.BookSortListType;
import com.darly.book.presenter.BookSortListPresenter;
import com.darly.book.presenter.contract.BookSortListContract;
import com.darly.book.ui.activity.BookDetailActivity;
import com.darly.book.ui.adapter.BookSortListAdapter;
import com.darly.book.ui.base.BaseMVPFragment;
import com.darly.book.widget.RefreshLayout;
import com.darly.book.widget.adapter.WholeAdapter;
import com.darly.book.widget.itemdecoration.DividerItemDecoration;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;


public class BookSortListFragment extends BaseMVPFragment<BookSortListContract.Presenter>
        implements BookSortListContract.View {
    private static final String EXTRA_GENDER = "extra_gender";
    private static final String EXTRA_TYPE = "extra_type";
    private static final String EXTRA_MAJOR = "extra_major";

    /********************/
    RefreshLayout mRefreshLayout;
    RecyclerView mRvContent;
    /*************************************/
    BookSortListAdapter mBookSortListAdapter;
    /************************************/
    private String mGender;
    private String mMajor;
    private BookSortListType mType;
    private String mMinor = "";
    private int mStart = 0;
    private int mLimit = 20;

    public static Fragment newInstance(String gender, String major, BookSortListType type) {
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_GENDER, gender);
        bundle.putString(EXTRA_MAJOR, major);
        bundle.putSerializable(EXTRA_TYPE, type);
        Fragment fragment = new BookSortListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getContentId() {
        return R.layout.fragment_refresh_list;
    }

    @Override
    protected BookSortListContract.Presenter bindPresenter() {
        return new BookSortListPresenter();
    }

    @Override
    protected void initView(View root) {
        mRefreshLayout = root.findViewById(R.id.refresh_layout);
        mRvContent = root.findViewById(R.id.refresh_rv_content);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if (savedInstanceState != null) {
            mGender = savedInstanceState.getString(EXTRA_GENDER);
            mMajor = savedInstanceState.getString(EXTRA_MAJOR);
            mType = (BookSortListType) savedInstanceState.getSerializable(EXTRA_TYPE);
        } else {
            mGender = getArguments().getString(EXTRA_GENDER);
            mMajor = getArguments().getString(EXTRA_MAJOR);
            mType = (BookSortListType) getArguments().getSerializable(EXTRA_TYPE);
        }

    }

    @Override
    protected void initClick() {
        super.initClick();
        mBookSortListAdapter.setOnItemClickListener(
                (view, pos) -> {
                    String bookId = mBookSortListAdapter.getItem(pos).get_id();
                    BookDetailActivity.startActivity(getContext(), bookId);
                }
        );

        mBookSortListAdapter.setOnLoadMoreListener(
                () -> mPresenter.loadSortBook(mGender, mType, mMajor, mMinor, mStart, mLimit)
        );

        //子类的切换
        Disposable disposable = BookController.getInstance()
                .toObservable(BookSubSortEvent.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (event) -> {
                            mMinor = event.bookSubSort;
                            mRefreshLayout.showLoading();
                            mStart = 0;
                            mPresenter.refreshSortBook(mGender, mType, mMajor, mMinor, mStart, mLimit);
                        }
                );
        addDisposable(disposable);
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        setUpAdapter();
    }

    private void setUpAdapter() {
        mBookSortListAdapter = new BookSortListAdapter(getContext(), new WholeAdapter.Options());

        mRvContent.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvContent.addItemDecoration(new DividerItemDecoration(getContext()));
        mRvContent.setAdapter(mBookSortListAdapter);
    }

    @Override
    protected void processLogic() {
        super.processLogic();
        mRefreshLayout.showLoading();
        mPresenter.refreshSortBook(mGender, mType, mMajor, mMinor, mStart, mLimit);
    }

    @Override
    public void finishRefresh(List<SortBookBean> beans) {
        if (beans.isEmpty()) {
            mRefreshLayout.showEmpty();
            return;
        }
        mBookSortListAdapter.refreshItems(beans);
        mStart = beans.size();
    }

    @Override
    public void finishLoad(List<SortBookBean> beans) {
        mBookSortListAdapter.addItems(beans);
        mStart += beans.size();
    }

    @Override
    public void showError() {
        mRefreshLayout.showError();
    }

    @Override
    public void showLoadError() {
        mBookSortListAdapter.showLoadError();
    }

    @Override
    public void complete() {
        mRefreshLayout.showFinish();
    }

    /***************************************************************/
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(EXTRA_GENDER, mGender);
        outState.putString(EXTRA_MAJOR, mMajor);
        outState.putSerializable(EXTRA_TYPE, mType);
    }
}
